#include "utils.h"

#include <QDir>
#include <ctime>

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif  // _WIND32

namespace PCSG {


Utils::Utils()
{

}

bool PCSG::Utils::checkFolder(const QString& folder)
{
    QDir dir(folder);
    if(dir.exists())
    {
      return true;
    }
    return dir.mkdir(folder);
}

int64_t Utils::getSysTimeByMs()
{
#ifdef _WIN32
#define EPOCHFILETIME   (116444736000000000UL)
    FILETIME ft;
    LARGE_INTEGER li;
    int64_t tt = 0;
    GetSystemTimeAsFileTime(&ft);
    li.LowPart = ft.dwLowDateTime;
    li.HighPart = ft.dwHighDateTime;

    //tt = (li.QuadPart - EPOCHFILETIME) /10;
    tt = (li.QuadPart - EPOCHFILETIME) /10000;
    return tt;
#else
    timeval tv;
    gettimeofday(&tv, 0);
    //return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
    return ((int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec)/1000;
#endif // _WIN32
    return 0;
}

QString Utils::getTimeStrByS()
{
    const time_t t = time(NULL);
    struct tm* current_time = localtime(&t);

    return QString("%1-%2-%3-%4-%5-%6")
            .arg(current_time->tm_year + 1900)
            .arg(current_time->tm_mon + 1)
            .arg(current_time->tm_mday)
            .arg(current_time->tm_hour)
            .arg(current_time->tm_min)
            .arg(current_time->tm_sec);
}



}// namespace PCSG
